package com.cams.backend.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cams.backend.common.dto.LoginDto;
import com.cams.backend.common.lang.Result;
import com.cams.backend.entity.Users;
import com.cams.backend.entity.Work;
import com.cams.backend.mapper.UsersMapper;
import com.cams.backend.service.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cams.backend.util.JwtUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cams
 * @since 2022-05-10
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired
    JwtUtils jwtUtils;

    @Resource
    private UsersMapper usersMapper;

    /**
     * 添加用户
     * 方法实现
     * @author chs
     */

    @Override
    public Result newUser(Users user) {
        QueryWrapper<Users> userQueryWrapper = Wrappers.query();
        userQueryWrapper.eq("uid", user.getUid() );
        Users temp = usersMapper.selectOne(userQueryWrapper);
        if (temp != null) {
            return Result.fail("该用户名已被注册！");

        } else {
            usersMapper.insert(user);
            return Result.succeed(MapUtil.builder()
                    .put("uid", user.getUid())
                    .put("name", user.getName())
                    .put("identity",user.getIdentity())
                    .map());
        }

    }

    /**
     * 用户登录
     * 方法实现
     * @author chs
     */
    @Override
    public Result login(LoginDto loginDto, HttpServletResponse response) {
        Users user = usersMapper.selectOne(new QueryWrapper<Users>().eq("uid", loginDto.getUid()));
        Assert.notNull(user, "用户不存在");

        if(!user.getPassword().equals(loginDto.getPassword())){
            return Result.fail("密码不正确");
        }
        String jwt = jwtUtils.generateToken(user.getUid());

        response.setHeader("Authorization", jwt);
        response.setHeader("Access-control-Expose-Headers", "Authorization");

        return Result.succeed(MapUtil.builder()
                .put("uid", user.getUid())
                .put("name", user.getName())
                .put("identity",user.getIdentity())
                .map()
        );
    }

    /**
     * 用户登出
     * 函数声明
     */

    @Override
    public Result logout(){
        SecurityUtils.getSubject().logout();
        return Result.succeed(null);
    }


    /**
     * 查看个人信息
     * 方法实现
     */
    @Override
    public Result findByUid(int uid) {
        Users user = usersMapper.selectOne(new QueryWrapper<Users>().eq("uid", uid));
        if(user==null){
            return Result.fail("用户不存在");
        }else{
            return Result.succeed(user);
        }
    }

    /**
     * 用户删除
     * @param uid
     * @return
     */

    @Override
    public Result deleteUserByUid(int uid) {
        usersMapper.delete(new QueryWrapper<Users>().eq("uid", uid));
        return Result.succeed(null);
    }



    /**
     * 修改账户信息
     * 方法实现
     */
    @Override
    public Result updateUserInfo(Users user) {
        usersMapper.update(user,new QueryWrapper<Users>().eq("uid", user.getUid()));
        return Result.succeed(user);

    }

    /**
     * 查看普通用户列表
     * 方法实现
     * @author cams
     */
    @Override
    public Result allUsers() {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .ne("identity", "admin");
        List<Users> oldUsersList = usersMapper.selectList(queryWrapper);
        return Result.succeed(oldUsersList);
    }
}
